package com.coocaa.launcher.wallpaper;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Interpolator;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.coocaa.x.framework.app.CoocaaApplication;

/**
 * Created by luwei on 15-9-10.
 */
public class AnimationButton extends FrameLayout implements View.OnFocusChangeListener{

    private Button button = null;
    private TextView textView = null;
    private Context context = null;
    private ObjectAnimator alpha = null;
    private ObjectAnimator translationY = null;

    public AnimationButton(Context context) {
        super(context);
        this.context = context;
        addTextView();
        addButton();

    }


    private void addButton(){
        button = new Button(context);
        LayoutParams params = new LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.topMargin = CoocaaApplication.Div(100);
        addView(button, params);
        button.setOnFocusChangeListener(this);
    }

    public void setOnClickListener(OnClickListener listener){
        button.setOnClickListener(listener);
    }

    public Button getButton(){
        return button;
    }


    private void addTextView(){
        textView = new TextView(context);
        LayoutParams params = new LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
        addView(textView, params);
        textView.setVisibility(View.GONE);
    }

    public void setBackgroundId(int id){
        button.setBackgroundResource(id);
    }

    public void setTextSize(float size){
        textView.setTextSize(size);
    }

    public void setTextColor(int color){
        textView.setTextColor(color);
    }

    public void setText(int id){
        textView.setText(id);
    }

    public void setText(String txt){
        textView.setText(txt);
    }


    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus){
            textView.setVisibility(View.GONE);
        }
    }

    public void startTextViewAnimation(){
        textView.setVisibility(View.VISIBLE);
        if (alpha == null){
            alpha = ObjectAnimator.ofFloat(textView,"alpha",0.9f,1.0f,0);
            alpha.setInterpolator(new MyAccelerateDecelerateInterpolator());
            alpha.setDuration(800);
            translationY = ObjectAnimator.ofFloat(textView,"translationY",CoocaaApplication.Div(100),0);
            translationY.setInterpolator(new MyAccelerateDecelerateInterpolator());
            translationY.setDuration(800);
        }
        alpha.start();
        translationY.start();
        translationY.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                textView.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

    }

    public class MyAccelerateDecelerateInterpolator implements Interpolator {
        public MyAccelerateDecelerateInterpolator() {
        }

        @SuppressWarnings({"UnusedDeclaration"})
        public MyAccelerateDecelerateInterpolator(Context context, AttributeSet attrs) {
        }

        public float getInterpolation(float input) {
            float temp = (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;
            if (temp < 0.2f)
                temp += 0.05;
            return temp;
        }
    }
}
